import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/data/xian_huo_data_entity.dart';
import 'package:lcapp/page/aMapView/amap_tool.dart';
import 'package:lcapp/tools/string_tool.dart';
import 'package:lcapp/widget/asr_cache_image.dart';

class XianHuoItem extends StatefulWidget {
  final XianHuoDataEntity entity;
  final Function chooseFun;
  const XianHuoItem({Key? key, required this.entity, required this.chooseFun}) : super(key: key);

  @override
  _XianHuoItemState createState() => _XianHuoItemState();
}

class _XianHuoItemState extends State<XianHuoItem> {
  late double disTance;
  late String location;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('arr====='+widget.entity.address.toString());
    if(widget.entity.address!=null&&widget.entity.address!.isNotEmpty){
      List arr = widget.entity.address!.split(' ');
      print('arr====='+arr.toString());
      if(arr.length>=4){
        location = '${arr[0]??''} ${arr[1]??''} ${arr[2]??''}';
      }else{
        location = widget.entity.address!;
      }
    }
    if(widget.entity.longitude!=null&&widget.entity.latitude!=null&&widget.entity.longitude!.isNotEmpty&&widget.entity.latitude!.isNotEmpty){
      try{
        // disTance = AmapTool.getDistance(double.parse(widget.entity.latitude!), double.parse(widget.entity.longitude!));
      }catch(e){
        // disTance=null;
      }
    }
  }

  @override
  void didUpdateWidget(covariant XianHuoItem oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    // if(widget.entity.address!=null&&widget.entity.address!.isNotEmpty){
    //   List arr = widget.entity.address!.split(' ');
    //   print('arr====='+arr.toString());
    //   if(arr.length>=4){
    //     location = '${arr[0]??''} ${arr[1]??''} ${arr[2]??''}';
    //   }else{
    //     location = widget.entity.address;
    //   }
    // }
    // if(widget.entity.longitude!=null&&widget.entity.latitude!=null&&widget.entity.longitude.isNotEmpty&&widget.entity.latitude.isNotEmpty){
    //   try{
    //     disTance = AmapTool.getDistance(double.parse(widget.entity.latitude), double.parse(widget.entity.longitude));
    //   }catch(e){
    //     disTance=null;
    //   }
    // }
    // setState(() {
    //
    // });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        if(widget.chooseFun!=null){
          widget.chooseFun();
          return;
        }
        Navigator.of(context).pushNamed('XianHuoDetailPage',arguments: widget.entity.id);
      },
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Container(
          color: Colors.white,
          child: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Visibility(child: ASRCacheImage(
                    image: widget.entity.images!=null&&widget.entity.images!.isNotEmpty?widget.entity.images![0]:'null',
                    fit: BoxFit.fitWidth,
                    // height: 300.w,
                    width: double.infinity,
                  ),visible: widget.entity.images!=null,),
                  Container(
                    padding: EdgeInsets.all(20.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Row(
                            children: [
                              Visibility(child: Container(
                                width:70.w,
                                height: 40.w,
                                padding: EdgeInsets.only(bottom: 2.w),
                                margin: EdgeInsets.only(right: 10.w),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: Color(0xffFF7835),
                                  borderRadius: BorderRadius.circular(10.w)
                                ),
                                child: Text('采购',style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 24.sp
                                ),),
                              ),visible: widget.entity.publishType==1,),
                              Expanded(child: Text(widget.entity.name??'',style: TextStyle(
                                  color:Colors.black,
                                  fontSize: 24.sp,
                                  fontWeight: FontWeight.w500
                              ),))
                            ],
                          ),
                        ),
                        Container(
                            margin: EdgeInsets.symmetric(vertical: 20.w),
                            child: Row(
                              children: [
                                Expanded(child: RichText(
                                  maxLines: 1,
                                  softWrap: true,
                                  text: TextSpan(
                                    text: '￥${widget.entity.price}',
                                    style: TextStyle(
                                        fontSize: 30.sp,
                                        color: const Color(0xffFF1818),
                                        fontWeight: FontWeight.w600
                                    ),
                                    children: <TextSpan>[
                                      TextSpan(
                                        text: ' ${StringTool.getDWstr(widget.entity.productUnit!)}',
                                        style:  TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 20.sp,
                                        ),
                                      ),
                                    ],
                                  ),
                                  textAlign: TextAlign.left,
                                )),
                              ],
                            )
                        ),
                        Container(
                          height: 1,
                          margin: EdgeInsets.only(bottom: 20.w),
                          color: Color(0xffE9E8E9),
                        ),
                        Container(
                          child: Row(
                            children: [
                              Image.asset('assets/home/xh_time_year.png',width: 24.w,),
                              SizedBox(width: 10.w,),
                              Text(widget.entity.updateTime??'',style: TextStyle(
                                  color:Color(0xff333333),
                                  fontSize: 20.sp
                              ),),
                              Expanded(child: Container()),
                              Text('${widget.entity.numbers}吨',style: TextStyle(
                                  color:Color(0xff333333),
                                  fontSize: 20.sp
                              ),),
                            ],
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 20.w,bottom: 0.w),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Image.asset('assets/fabu/fabu_dt.png',width: 24.w,),
                                SizedBox(width: 10.w,),
                                Expanded(child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      child: Text(location??'',overflow: TextOverflow.clip,maxLines:1
                                      ,style: TextStyle(
                                          color:Color(0xff333333),
                                          fontSize: 20.sp
                                      ),),),
                                    Container(
                                        margin: EdgeInsets.only(top: 10.w,bottom: 0.w),
                                        child: Visibility(
                                          child: Row(
                                            children: [
                                              Expanded(child: Text('距离${disTance}km',overflow: TextOverflow.clip,maxLines:1
                                                ,style: TextStyle(
                                                    color:Color(0xff7B7B7B),
                                                    fontSize: 20.sp
                                                ),)),
                                            ],
                                          ),
                                          visible: disTance!=null,
                                        )
                                    ),
                                  ],
                                )),
                              ],
                            )
                        ),
                        
                      ],
                    ),
                  )
                ],
              ),
              Visibility(child: Positioned(
                child: GestureDetector(
                  onTap: (){
                    if(widget.chooseFun!=null){
                      widget.chooseFun();
                    }
                  },
                  child: Icon(widget.entity.isChoose==true?Icons.check_box_rounded:Icons.crop_square,color: widget.entity.isChoose==true?Colors.green:Color(0xffeeeeee),size: 60.w,),
                ),
                right: 0,top: 0,),visible: widget.chooseFun!=null,),
            ],
          )
        ),
      ),
    );
  }
}
